<?php
define( '__ESPACE__', 'admin');
$prefix = '../../';
require_once($prefix.'starter.php');

ob_start();

$PWE_admin = new controler_admin();

$mode = (isset($_GET)&&isset($_GET['action']))?$_GET['action']:'create';
$modele = 'publication';
$modele_id = 0;
$ligne_id = (isset($_GET)&&isset($_GET['ligne_id']))?$_GET['ligne_id']:0;

if(isset($_GET)&&isset($_GET['lien'])&& $_GET['lien']!==''){
    $redirection = new modele_redirection($_GET['lien']);
    $modele = $redirection->getModele();
    $modele_id = $redirection->getModeleId();
}elseif(isset($_GET)&&isset($_GET['modele_id'])&& $_GET['modele_id']>0){
    $redirection = new modele_redirection();
    $modele_id = $_GET['modele_id'];
    $modele = $_GET['modele'];
}else{
    
}

?>
<!DOCTYPE html>
<html>
    <head>
        <title>Ajouter/modifier une redirection</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="robots" content="noindex,nofollow">
        <link href='http://fonts.googleapis.com/css?family=Grand+Hotel' rel='stylesheet' type='text/css'>
        <link href="../css/style.css" type="text/css" rel="stylesheet" media="all"/>
        <script src="../../js/libs/jquery/jquery.js" type="text/javascript"></script>
        <link href="<?php echo $PWE_admin->getUiThemePath(); ?>" type="text/css" rel="stylesheet" media="all"/>
        <script src="../../js/libs/jqueryui/jquery-ui.js" type="text/javascript"></script>
    <?php if($mode !== 'delete'): ?>
    <?php if(isset($_POST) && isset($_POST['submit'])): ?>
        <?php
            $lien = $_POST['modele_lien'];
            $code = $_POST['modele_code'];
            
            $redirection->setDatas(array(
                'lien'=>$lien,
                'code'=>$code,
                'modele'=>$modele,
                'modele_id'=>$modele_id
            ));
            if($mode==='create'){
                $redirection->save();
            }else{
                $redirection->update();
            }
        ?>
        <script type="text/javascript">
            var origine = window.top;
            <?php if($mode === 'create'): ?>
            $(document).ready(function(){
                origine.addRedirection({modele: '<?php echo $modele; ?>', modele_id: <?php echo $modele_id; ?>, lien: "<?php echo $lien; ?>", code: "<?php echo $code; ?>"});
            });
            <?php elseif($mode === 'update'): ?>
                origine.updateRedirection({ligne_id: <?php echo $ligne_id; ?>, modele: '<?php echo $modele; ?>', modele_id: <?php echo $modele_id; ?>, lien: "<?php echo $lien; ?>", code: "<?php echo $code; ?>"});
            <?php endif; ?>
        </script>
    </head>
    <body>
        modification/création effectuée
    </body>
    <?php else: ?>
        <script type="text/javascript">
            var origine = window.top;    
            $(document).ready(function(){
                $('.pwe-ui-btn').button();
                $('form').submit(function(){
                    if( $('#lien').val() !== '' ){
                        return true;
                    }else{
                        return false;
                    }
                });
            });
        </script>
    </head>
    <body>
        <form method="post" enctype="multipart/form-data" action="?action=<?php echo $mode; ?>&modele=<?php echo $modele; ?>&modele_id=<?php echo $modele_id; ?>&lien=<?php echo $redirection->getLien(); ?>">
            <label>lien : </label><input id="lien" type="text" name="modele_lien" value="<?php echo $redirection->getLien(); ?>" /><br/>
            <label>code : </label><input id="code" type="text" name="modele_code" value="301" /><br/>
            <input id="submit"  type="submit" name="submit" value="<?php echo ($mode==='create')?'Ajouter':'Modifier'; ?>" class="pwe-ui-btn"/>
        </form>
    </body>
    <?php endif; ?>
</html>
<?php else: ?>
    <?php
    $lien = $redirection->getLien();
    $redirection->delete();
    ?>
        <script type="text/javascript">
            var origine = window.top;
            
            $(document).ready(function(){
                origine.removeRedirection({ligne_id: <?php echo $ligne_id; ?>, modele: '<?php echo $modele; ?>',lien: "<?php echo $lien; ?>", modele_id: <?php echo $modele_id; ?>});
            });
        </script>
    </head>
    <body>
        suppression terminée
    </body>
</html>
<?php endif; ?>
<?php ob_end_flush(); ?>